package com.moleq.mgpos.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.models.IDevModel;
import com.moleq.mgpos.activities.R;

public class HardwareListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<IDevModel> dataSource;
	private int selectedRow = 0;

	public HardwareListAdapter(Context context, Object Source) {
		this.mInflater = LayoutInflater.from(context);

		this.setSource(Source);

	}

	@SuppressWarnings("unchecked")
	public void setSource(Object Source) {

		dataSource = (ArrayList<IDevModel>) Source;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataSource.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataSource.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.hardware_setting_row, null);
			holder.tv_scanner_items_scanner = (TextView) convertView.findViewById(R.id.tvHardwareDevice);
			holder.tv_scanner_items_port = (TextView) convertView.findViewById(R.id.tvHardwarePort);
			holder.tv_scanner_items_device_name = (TextView) convertView.findViewById(R.id.tvHardwareName);
			holder.iv_scanner_items_active = (ImageView) convertView.findViewById(R.id.ivHardwareActive);
			holder.ll_scanner_items_layout = (LinearLayout) convertView.findViewById(R.id.llHardwareRow);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		IDevModel dev = dataSource.get(position);
		holder.tv_scanner_items_scanner.setText(dev.getDevice());
		holder.tv_scanner_items_port.setText(String.valueOf(dev.getPort()));
		holder.tv_scanner_items_device_name.setText(dev.getDeviceName());

		if (dev.isActive()) {
			holder.iv_scanner_items_active.setBackgroundResource(R.drawable.check_true);
		}else{
			holder.iv_scanner_items_active.setBackgroundResource(R.drawable.check_false);
		}

		if (position == selectedRow) {
			// holder.tv_scanner_items_scanner.setBackgroundColor(Color.rgb(255,219,16));
			// holder.tv_scanner_items_port.setBackgroundColor(Color.rgb(255,219,16));
			// holder.tv_scanner_items_device_name.setBackgroundColor(Color.rgb(255,219,16));
			holder.ll_scanner_items_layout.setBackgroundColor(Color.rgb(255, 219, 16));
		} else {
			// holder.tv_scanner_items_scanner.setBackgroundColor(Color.rgb(204,
			// 204, 204));
			// holder.tv_scanner_items_port.setBackgroundColor(Color.rgb(204,
			// 204, 204));
			// holder.tv_scanner_items_device_name.setBackgroundColor(Color.rgb(204,
			// 204, 204));
			holder.ll_scanner_items_layout.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;

	}

	public final class ViewHolder {
		TextView tv_scanner_items_scanner;
		TextView tv_scanner_items_port;
		TextView tv_scanner_items_device_name;
		ImageView iv_scanner_items_active;
		LinearLayout ll_scanner_items_layout;
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}
}
